﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary;
using CommonLibrary.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;

namespace BusinessLibrary
{
    /// <summary>
    /// 出站类
    /// </summary>
    public class Outbound : IPlugin
    {
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description => "出站相关业务处理";
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        public string Excute(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            if (pluginParam != null
                && APPInstance.PLC != null
                && APPInstance.PLC.IsConnected)
            {
                string identity = pluginParam.Identity.ToUpper();
                switch (identity)
                {
                    case "OUTBOUND_DATA": result = OutboundData(pluginParam); break;
                    case "OUTBOUND_FINISH": result = OutboundFinishData(pluginParam); break;
                }
            }
            return result;
        }
        /// <summary>
        /// 返回一个窗口
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>

        public HWFrmWithTitle GetForm(PluginParamEntity pluginParam)
        {
            return null;
        }
        /// <summary>
        /// 返回一个 Bitmap
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            return null;
        }
        /// <summary>
        /// OUTBOUND_FINISH 出站完成
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string OutboundFinishData(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "出站完成";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var entity = CommonHelper.GetJson(pluginParam.ParamString, new OutboundFinishExcuteEntity());
                if (entity != null)
                {
                    try
                    {
                        APPInstance.PLC.Write(entity.DoneAddress, (short)1);
                        APPInstance.PLC.Write(entity.TriggerAddress, (short)0);
                        this.Info($"【{pluginParam.Tag}】处理 - {methodName} 写入出站完成PLC交互");
                    }
                    catch (Exception ex)
                    {
                        this.Error(ex);
                    }
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return result;
        }
        /// <summary>
        /// OUTBOUND_DATA 出站
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string OutboundData(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "出站";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var entity = CommonHelper.GetJson(pluginParam.ParamString, new OutboundExcuteEntity());
                if (entity != null && entity.PLCTags != null && entity.PLCTags.Count > 0)
                {
                    try
                    {
                        Stopwatch plcWatch = Stopwatch.StartNew();
                        this.Info($"{entity.Remark} 开始读取PLC数据");
                        Dictionary<string, object> outDic = new Dictionary<string, object>();
                        outDic = APPInstance.PLC.BatchRead(entity.PLCTags);
                        plcWatch.Stop();
                        this.Info($"{entity.Remark} 结束读取PLC数据，耗时：{plcWatch.ElapsedMilliseconds} 毫秒");

                        bool isMESOffline = MesCommonHelper.IsMESOffline;
                        bool isOk = false;
                        if (entity.OutType == EOutType.FINAL)
                        {
                            ProductInfo p1 = ProductInfo.FromDic(outDic);
                            p1.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            isOk = entity.Remark.ToUpper().StartsWith("OK");
                            p1.IsOKOut = isOk ? "1" : "0";
                            p1.IsMESOffline = isMESOffline ? "1" : "0";
                            this.Info($"{entity.Remark} 出站数据为：\r\n{CommonHelper.GetJsonString(p1)}");

                            //MES交互
                            //出站上传
                            var mesResult = !isMESOffline ? MesBusiness.OutCheck(p1) : new MESResult()
                            {
                                Code = "200",
                                Success = true,
                                Message = "MES离线"
                            };
                            var mesValue = mesResult != null && mesResult.IsSuccessReturn ? true : false;
                            //MES结果参数上传
                            var mesResultParamResult = !isMESOffline ? MesBusiness.DeviceResultCheck(p1) : new MESResult()
                            {
                                Code = "200",
                                Success = true,
                                Message = "MES离线"
                            };
                            var mesUpload = isMESOffline ? false : mesValue;
                            p1.HasMESUpload = mesUpload ? "1" : "0";
                            ProductRecordManager.Instance.AddHeliumData(p1);
                            //写PLC
                            var mesAddress = entity.ResultAddress;
                            APPInstance.PLC.Write(mesAddress, mesValue ? (short)1 : (short)0);
                        }
                        string eachMessage = $"{entity.Remark} 电池{entity.Index}采集完毕。";
                        if (PluginParam.ShowRunningLog != null)
                        {
                            PluginParam.ShowRunningLog(isOk, eachMessage);
                        }
                        this.Info($"【{pluginParam.Tag}】处理 - {methodName} {eachMessage}");
                    }
                    catch (Exception ex)
                    {
                        this.Error(ex);
                    }
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return result;
        }
    }
}
